using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace YDT_ABPvNext_Projects.Data;

public class YDT_ABPvNext_ProjectsDbContextFactory : IDesignTimeDbContextFactory<YDT_ABPvNext_ProjectsDbContext>
{
    public YDT_ABPvNext_ProjectsDbContext CreateDbContext(string[] args)
    {
        YDT_ABPvNext_ProjectsEfCoreEntityExtensionMappings.Configure();

        var configuration = BuildConfiguration();

        var builder = new DbContextOptionsBuilder<YDT_ABPvNext_ProjectsDbContext>()
            .UseMySql(configuration.GetConnectionString("Default"), MySqlServerVersion.LatestSupportedServerVersion);

        return new YDT_ABPvNext_ProjectsDbContext(builder.Options);
    }

    private static IConfigurationRoot BuildConfiguration()
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false);

        return builder.Build();
    }
}
